package cn.ssq.service.impl;


import cn.ssq.dao.*;
import cn.ssq.domain.Result;
import cn.ssq.domain.SampleInfo;
import cn.ssq.service.SampleInfoService;
import cn.ssq.service.SampleToxinInfoService;
import cn.ssq.util.DateUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class SampleInfoServiceImpl implements SampleInfoService {

    @Autowired
    private SampleInfoMapper sampleInfoMapper;
    @Autowired
    private AddressCityMapper addressCityMapper;
    @Autowired
    private AddressProvinceMapper addressProvinceMapper;
    @Autowired
    private AddressTownMapper addressTownMapper;
    @Autowired
    private SampleToxinMapper sampleToxinMapper;
    @Autowired
    private BacterialStrainInfoMapper bacterialStrainInfoMapper;
    @Autowired
    private SampleToxinInfoService sampleToxinInfoService;
    //向数据库中添加新的数据
    @Override
    public int addSampleInfo(Map<String ,Object> map ) {
            //样品编号
        String sampleId = (String) map.get("sampleId");
        String cropCategory = (String) map.get("cropCategoryId");
        Integer cropCategoryId = Integer.valueOf(cropCategory);
        String provinceCode = (String) map.get("sheng");
        String province = addressProvinceMapper.selectAddressProvinceBycode(provinceCode);
        String city1 = (String) map.get("shi");
        String city = addressCityMapper.selectCityBycode(city1);
        String countyCode = (String) map.get("xian");
        String county = addressTownMapper.selectAddressTownBycode(countyCode);
        String harvestTime = (String) map.get("harvest_time");
        String samplingTime = (String) map.get("sampling_time");
        String samplingPeople = (String) map.get("samplingPeople");
        String seasonal = (String) map.get("seasonal");
        String description = (String) map.get("description");
        String pollutionRate = (String) map.get("pollutionRate");
        String breed1 = (String) map.get("cropCpeciesId");
        Integer breed = Integer.parseInt(breed1);
        String township = (String) map.get("xiang");
        String village = (String) map.get("cun");
        String household = (String) map.get("hu");
        Integer isdel = 0;
        SampleInfo sampleInfo = new SampleInfo();
        sampleInfo.setSeasonal(seasonal);
        sampleInfo.setDescription(description);
        sampleInfo.setSampleId(sampleId);
        sampleInfo.setCropCategoryId(cropCategoryId);
        sampleInfo.setBreed(breed);
        sampleInfo.setProvince(province);
        sampleInfo.setCity(city);
        sampleInfo.setCounty(county);
        sampleInfo.setTownship(township);
        sampleInfo.setVillage(village);
        sampleInfo.setHousehold(household);
        Date hTime1 = null;
        try {
            hTime1 = DateUtils.string2Date(harvestTime, "yyyy-MM-dd");
        } catch (ParseException e) {
            e.printStackTrace();
        }
        sampleInfo.setHarvestTime(hTime1);
        Date sTime1 = null;
        try {
            sTime1 = DateUtils.string2Date(samplingTime, "yyyy-MM-dd");
        } catch (ParseException e) {
            e.printStackTrace();
        }
        sampleInfo.setSamplingTime(sTime1);
        sampleInfo.setSamplingPeople(samplingPeople);
        sampleInfo.setPollutionRate(Float.valueOf(pollutionRate));
        sampleInfo.setIsdel(isdel);
        int i1 = sampleInfoMapper.insertInfo(sampleInfo);

        Integer id = sampleInfo.getId();

        return id;
    }


        //根据id查询信息
    @Override
    public SampleInfo findById(Integer id) {
        SampleInfo sampleInfoList = sampleInfoMapper.selectBysampId(id);
        return sampleInfoList;
    }

    /**
     * 修改信息
     * @param map
     * @return
     */
    @Override
    public int updateSampleInfo(Map<String, Object> map) {
        //样品编号
        String sampleId = (String) map.get("sampleId");
        String cropCategory = (String) map.get("cropCategoryId");
        Integer cropCategoryId = Integer.valueOf(cropCategory);
        String provinceCode = (String) map.get("sheng");
        String province = addressProvinceMapper.selectAddressProvinceBycode(provinceCode);
        String city1 = (String) map.get("shi");
        String city = addressCityMapper.selectCityBycode(city1);
        String countyCode = (String) map.get("xian");
        String county = addressTownMapper.selectAddressTownBycode(countyCode);
        String harvestTime = (String) map.get("harvest_time");
        String samplingTime = (String) map.get("sampling_time");
        String samplingPeople = (String) map.get("samplingPeople");
        String seasonal = (String) map.get("seasonal");
        String description = (String) map.get("description");
        String pollutionRate = (String) map.get("pollutionRate");
        String breed1 = (String) map.get("cropCpeciesId");
        Integer breed = Integer.parseInt(breed1);
        String township = (String) map.get("xiang");
        String village = (String) map.get("cun");
        String household = (String) map.get("hu");
        String isdel = "0";
        String dd = (String) map.get("samid");
        int id= Integer.parseInt(dd);
        int i = sampleInfoMapper.updateByPrimaryKey(id, sampleId, province, city, county, cropCategoryId, breed, township, village, household, harvestTime, samplingTime,
                samplingPeople, seasonal, description, pollutionRate, isdel);


        return i;
    }

    @Override
    public Result delectSampleById(Integer[] ids) {
        Integer isdel = 1;
        int i =0;
        if (ids!=null&&ids.length>0){
            for (Integer id : ids) {
                 i = sampleInfoMapper.deleteSampleById(id, isdel);
            }
        }
        Result result = new Result();
        if (i==1){
            result.setFlag(true);
        }else {
            result.setFlag(false);
        }
        return result;
    }

    /**
     *
     * 校验样品编号是否可用
     * @return
     */
    @Override
    public String[] selectverifyUser() {

        String[] selectverifyUser = sampleInfoMapper.selectverifyUser();
        System.out.println(selectverifyUser);
        return selectverifyUser;

    }

    /**
     *
     * 导入增加到数据库
     * @param sampleId
     * @param isdel
     * @param
     * @param cropCpeciesId
     * @param sheng
     * @param shi
     * @param xian
     * @param xiang
     * @param cun
     * @param hu
     * @param hTime
     * @param sTime
     * @param samPeople
     * @param seasonal
     * @param description
     * @param pollutionRate
     * @param toxinId
     * @param word
     * @param txt
     * @param png
     * @return
     */
    @Override
    public int insertInfo(String sampleId, Integer isdel, Integer categoryId, Integer cropCpeciesId, String sheng, String shi, String xian, String xiang, String cun, String hu, String hTime, String sTime, String samPeople, String seasonal, String description, String pollutionRate, List<Integer>toxinId,String[] orgNum, String[] word, String[] txt, String[] png) throws ParseException {
        SampleInfo sampleInfo = new SampleInfo();
        sampleInfo.setSeasonal(seasonal);
        sampleInfo.setDescription(description);
        sampleInfo.setSampleId(sampleId);
        sampleInfo.setCropCategoryId(categoryId);
        sampleInfo.setBreed(cropCpeciesId);
        sampleInfo.setProvince(sheng);
        sampleInfo.setCity(shi);
        sampleInfo.setCounty(xian);
        sampleInfo.setTownship(xiang);
        sampleInfo.setVillage(cun);
        sampleInfo.setHousehold(hu);
        Date hTime1 = null;
        try {
            hTime1 = DateUtils.string2Date(hTime, "yyyy-MM-dd");
        } catch (ParseException e) {
            e.printStackTrace();
        }
        sampleInfo.setHarvestTime(hTime1);
        Date sTime1 = DateUtils.string2Date(sTime, "yyyy-MM-dd");
        sampleInfo.setSamplingTime(sTime1);
        sampleInfo.setSamplingPeople(samPeople);
        sampleInfo.setPollutionRate(Float.valueOf(pollutionRate));
        sampleInfo.setIsdel(isdel);
        int i = sampleInfoMapper.insertInfo(sampleInfo);
        Integer id = sampleInfo.getId();
       // int sid = sampleInfoMapper.selectById(sampleId);
        for (int j = 0; j < toxinId.size(); j++) {
            sampleToxinMapper.insertToxn(toxinId.get(j),null,id);
        }
        if (orgNum.length>0&&word.length>0&&txt.length>0&&png.length>0){
            for (int i1 = 0; i1 < orgNum.length; i1++) {
                bacterialStrainInfoMapper.insert(id,sampleId,orgNum[i1],word[i1],txt[i1],png[i1],isdel);

            }
        }


        return 1;
    }

    @Override
    public List<SampleInfo> selectLikePage(Map<String, Object> map) {
        return null;
    }


    /**
     *
     *
   */



    @Override
    public List<SampleInfo> findSampleInfoList() {
        Integer isdel =0;
        return sampleInfoMapper.findSampleInfoList(isdel);
    }

    /**
     * 模糊查询

     * @return
     */
    @Override
    public PageInfo selectLikeSampleId(Map<String,Object> map,Integer pageNum) {
        String year = (String) map.get("year");

        if (year==null||year.equals("0")){
            year=null;
        }
        String month = (String) map.get("month");
        if (month==null||month.equals("0")){
            month=null;
        }

        String day = (String) map.get("day");
        if (day==null||day.equals("0")){
            day=null;
        }
        String sampleId = (String) map.get("sampleId");
        if (sampleId==null||sampleId.equals("")){
            sampleId=null;
        }
        String wrl = (String) map.get("wrl");
        String leftStr =null;
        String rightStr =null;
        if (wrl == null || wrl.equals("")) {
            wrl=null;
        } else {
            if (wrl.length() == 0) {
                wrl=null;
            } else {
                String[] strs = wrl.split("-");
                leftStr = strs[0];
                rightStr = strs[1];
            }
        }
        String toxinTyoe = (String) map.get("toxinTyoe");

        if (toxinTyoe==null||toxinTyoe.equals("")){
            toxinTyoe=null;
        }
        String cropSpecies = (String) map.get("cropSpecie");
        if ( cropSpecies==null||cropSpecies.equals("")){
            cropSpecies=null;
        }
        String shen1 = (String) map.get("shen");
        String shen = addressProvinceMapper.selectAddressProvinceBycode(shen1);
        if ( shen1==null||shen1.equals("")){
            shen1=null;
        }
        String shi1 = (String) map.get("shi");
        String shi = addressCityMapper.selectCityBycode(shi1);
        if (shi1==null||shi1.equals("0")){
            shi1=null;
        }
        String xian1 = (String) map.get("xian");
        String xian = addressTownMapper.selectAddressTownBycode(xian1);
        if (xian1==null||xian1.equals("0")){
            xian1=null;
        }
        //把年月日拼接
        String date = null;
        if (day == null) {
            if (month == null) {
                if (year != null) {
                    date = year;
                }
            } else {
                date = year + "-" + month;
            }
        } else {
            date = year + "-" + month + "-" + day;
        }
        Integer isdel=0;
        PageHelper.startPage(pageNum,5);
        List<SampleInfo> sampleInfos = sampleInfoMapper.selectLikeSampleId(sampleId, leftStr, rightStr, toxinTyoe, cropSpecies, shen, shi, xian, date,isdel);
        PageInfo<SampleInfo> pageInfo = new PageInfo<SampleInfo>(sampleInfos);
        return pageInfo;
    }
}
